Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parameterCount to optimize performance #591

Merged
merged 3 commits into from
Mar 24, 2025

Conversation

wojtekmaj
Copy link
Contributor

Now that body-parser targets Node.js 18, we can finally use String.split to count all the &s in parameterCount body. This makes the function roughly 2x faster in Chrome/Node.js engine (and about 2.5x faster in Safari/Bun engine), according to this jsperf: https://jsperf.app/niqepi/2

Fun fact: if you run this jsperf in Firefox, the old implementation will get speeds unmatched by any other engine, while the new one will be roughly 7x slower. Thankfully, that's only the case in Firefox.

a

Now that body-parser targets Node.js 18, we can finally use String.split to count all the '&'s in parameterCount body. This makes the function roughly 2x faster, according to this jsperf: https://jsperf.app/niqepi/2

Fun fact: if you run this jsperf in Firefox, the old implementation will get record-breaking speeds, while the _new_ one will be roughly 7x slower. Thankfully, both Chrome/Node and Bun/Safari engines show the opposite results.
Copy link
Member

@wesleytodd wesleytodd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't quite see the same results in the micro-benchmark, but it was still an improvement. Additionally it is a nice legibility improvement to simplify this method so even if it was relatively equivalent perf I would not mind merging it. Just going to let it sit to see if anyone else had input.

Copy link
Member

@UlisesGascon UlisesGascon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah!

Copy link
Member

@UlisesGascon UlisesGascon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the linter happy again :)

@UlisesGascon UlisesGascon self-assigned this Mar 24, 2025
@UlisesGascon UlisesGascon merged commit f27f2ce into expressjs:master Mar 24, 2025
12 checks passed
@wojtekmaj wojtekmaj deleted the faster-parameter-count branch March 24, 2025 18:32
@UlisesGascon UlisesGascon mentioned this pull request Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants